{% extends 'base.html' %}

{% block load_css %}
    <link href="/static/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
{% endblock %}

{% block mbx %}
    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-sm-4">
            <h2>资产用户展示</h2>
            <ol class="breadcrumb">
                <li>
                    <a href="{% url 'index' %}">首页</a>
                </li>
                <li>
                    <a href="">资源管理</a>
                </li>
                <li>
                    <a href="">资产用户展示</a>
                </li>
            </ol>
        </div>
    </div>
{% endblock %}

{% block body %}
    <div class="col-lg-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>资产用户展示 </h5>
            </div>
            <div class="ibox-content">
                <a class="btn btn-w-m btn-primary" href="{% url 'serveruser_add' %}">创建资产用户</a>
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th class="text-center">名称</th>
                        <th class="text-center">系统用户</th>
                        <th class="text-center">备注</th>
                        <th class="text-center">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for object in object_list %}
                        <tr>
                            <td class="text-center">{{ object.name }}</td>
                            <td class="text-center">{{ object.username }}</td>
                            <td class="text-center">{{ object.info }}</td>
                            <td class="text-center">
                                <a href="{% url 'serveruser_update' %}?id={{ object.id }}"
                                   class="btn btn-primary btn-sm">更新</a>
                                <a class="btn btn-danger btn-sm"
                                   onclick="serveruser_delete(this, {{ object.id }})">删除</a>
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
                <center>
                    <div class="btn-group">
                        {% if page_obj.has_previous %}
                            <a href="{% url 'serveruser_list' %}?page={{ page_obj.previous_page_number }}"
                               class="btn btn-white">上一页</a>
                        {% endif %}
                        {% for page in page_range %}
                            {% if page_obj.number == page %}
                                <a class="btn btn-white active">{{ page }}</a>
                            {% else %}
                                <a class="btn btn-white"
                                   href="{% url 'serveruser_list' %}?page={{ page }}">{{ page }}</a>
                            {% endif %}
                        {% endfor %}
                        {% if page_obj.has_next %}
                            <a href="{% url 'serveruser_list' %}?page={{ page_obj.next_page_number }}"
                               class="btn btn-white">下一页</a>
                        {% endif %}
                    </div>
                </center>
            </div>
        </div>
    </div>

{% endblock %}


{% block load_js %}
    <script src="/static/js/plugins/sweetalert/sweetalert.min.js"></script>
    <script>
        function serveruser_delete(this_button, id) {
            var data = {};
            data['id'] = id;
            data['csrfmiddlewaretoken'] = "{{ csrf_token }}";
            //获取删除的是哪个用户
            username = $(this_button).parent().parent().find('td').eq(1).html()
            swal({
                title: "删除资产用户" + username,
                text: "真的确定删除吗?",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: false
            }, function () {
                $.ajax({
                    url: '{% url 'serveruser_delete' %}',
                    type: 'post',
                    data: data,
                    success: function (res) {
                        if (res.status == 0) {
                            swal({
                                title: res.msg,
                                type: 'success',
                                confirmButtonText: "OK"
                            }, function () {
                                window.location.reload()
                            });
                        } else {
                            swal({
                                title: res.msg,
                                type: 'error',
                                confirmButtonText: "知道了"
                            });
                        }
                    }
                })
            })
        };
    </script>
{% endblock %}